package com.brooks.demo;

import java.util.Random;
import java.util.concurrent.Phaser;
import java.util.concurrent.TimeUnit;

/**
 * Phaser分阶段执行
 *
 * @Author mihutu
 * @Date 2020/10/19 10:05
 * @Version 1.0
 */
public class DemoPhaser {
    static Random random = new Random();
    static MarryPhaser phaser = new MarryPhaser();
    /**
     * 线程睡觉
     *
     * @param time 毫秒
     * @throws InterruptedException
     */
    static void mSleep(int time) {
        try {
            TimeUnit.MILLISECONDS.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        phaser.bulkRegister(10);
        for(int i=0 ;i<8;i++){
            new Thread(new Person("客人"+i)::step).start();
        }
        new Thread(new Person("新郎")::step).start();
        new Thread(new Person("新娘")::step).start();
    }

    static class Person extends Thread {
        String name;

        public Person(String name) {
            this.name = name;
        }

        public void arrive() {
            mSleep(random.nextInt(1000));
            System.out.printf("%s 到达现场！\n", name);
            phaser.arriveAndAwaitAdvance();
        }

        public void eat() {
            mSleep(random.nextInt(1000));
            System.out.printf("%s 吃喜酒\n", name);
            phaser.arriveAndAwaitAdvance();
        }

        public void leave() {
                mSleep(random.nextInt(1000));
                System.out.printf("%s 离开了\n", name);
                phaser.arriveAndAwaitAdvance();
        }

        public void hug() {
            if(name.equals("新郎") || name.equals("新娘")){
                mSleep(random.nextInt(1000));
                System.out.printf("%s 拥抱\n", name);
                phaser.arriveAndAwaitAdvance();
            }else{
                //其他人解除注册
                phaser.arriveAndDeregister();
            }
        }
        public void step(){
            arrive();
            eat();
            leave();
            hug();
        }
    }

    static class MarryPhaser extends Phaser{
        /**
         *
         * @param phase 第几个阶段
         * @param registeredParties 有多少线程
         * @return
         */
        @Override
        protected boolean onAdvance(int phase, int registeredParties) {
            switch (phase){
                case 0:
                    System.out.println("所有人都到齐了" + registeredParties);
                    return false;
                case 1:
                    System.out.println("所有人吃完了" + registeredParties);
                    return false;
                case 2:
                    System.out.println("所有人都离开了" + registeredParties);
                    return false;
                case 3:
                    System.out.println("婚礼结束，新郎新娘爱的抱抱" + registeredParties);
                    return true;
                default:
                    System.out.println("默认输出");
                    return true;
            }
        }
    }
}
